package internal

import (
	"gorm.io/gorm"
	"server/app/g1/internal/model"
	db1 "server/libs/db"
)

func QueryUP(db *gorm.DB, id int64) ([]int64, error) {
	var UPIDArray []int64
	err := db.Debug().Raw("SELECT id_ as parent_id FROM( SELECT @pid AS id_,"+
		"( SELECT @pid := recommend_id FROM home_user_info WHERE id = id_ ) AS pid_ "+
		"FROM home_user_info,(SELECT @pid := ?) V WHERE @pid > 0) T, home_user_info N "+
		"WHERE T.id_ = N.id  LIMIT 6", id).
		Scan(&UPIDArray).Error
	if err != nil {
		return nil, err
	}
	return UPIDArray, nil
}
func QueryUPRegis(db *gorm.DB, id int64) ([]int64, error) {
	var UPIDArray []int64
	err := db.Debug().Raw("SELECT id_ as parent_id FROM( SELECT @pid AS id_,"+
		"( SELECT @pid := recommend_id FROM home_user_info WHERE id = id_ ) AS pid_ "+
		"FROM home_user_info,(SELECT @pid := ?) V WHERE @pid > 0) T, home_user_info N "+
		"WHERE T.id_ = N.id  LIMIT 6", id).
		Scan(&UPIDArray).Error
	if err != nil {
		return nil, err
	}
	return UPIDArray, nil
}
func LoadPledgeInfo(id int64, idList []int64, PledgeMap map[int64][]model.HomePledgeModel) {
	for _, value := range idList {
		var pledgeItem model.HomePledgeModel
		db1.Mysql().Debug().
			Model(&model.HomePledgeModel{}).
			Where("user_id = ? ", value).
			Order("id desc").
			Limit(1).
			Find(&pledgeItem)
		PledgeMap[id] = append(PledgeMap[id], pledgeItem)
	}
}
func GetPledgeInfoAll(id int64, PledgeMap map[int64][]model.HomePledgeModel) {
	data, _ := QueryUP(db1.Mysql(), id)
	LoadPledgeInfo(id, data, PledgeMap)
}
